public class Test2 {
    public static void main(String[] args) {
        Object A = new Object();
        Object B = new Object();
        Thread t1 = new Thread(() -> {
            synchronized (A) {
                //sleep一下，是给t2时间，让t2也能拿到B
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (B) {
                    System.out.println("t1拿到了两把锁！");
                }
            }
        });

        Thread t2 = new Thread(() -> {
            synchronized (B) {
                //sleep一下，是给t1时间，让t1也能拿到A
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (A) {
                    System.out.println("t2拿到了两把锁！");
                }
            }
        });

        t1.start();
        t2.start();
    }
}
